package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommSysCode.COMM_SYS_CODE;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.GroupField;
import org.jooq.SelectField;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.family.custom.CommSysCodeRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.SpaceConfInfoSearchRequest;
import com.tuanzi.loan.vo.response.family.SpaceConfInfoSearchResponse;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;


/**
 * 活动参与详情查询
 * @author JinRuiyang 2018/02/02
 */
@Repository
public class CommSysCodeRepositoryImpl extends JOOQBaseRepositoryPlus implements CommSysCodeRepositoryCustom {
    
	private static final String SPACE_TYPE = "advertise";
	private List<Integer> adList;
	
	public List<Integer> findAdvertiseIdList() {
		if(adList != null && adList.size() != 0) {
			return adList;
		}
		
		Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        selectFields.add(COMM_SYS_CODE.CODE);
        condition = condition.and(COMM_SYS_CODE.TYPE.eq(SPACE_TYPE));
        SelectQuery selectQuery = dslContext.select(selectFields)
        		.from(COMM_SYS_CODE)
                .where(condition)
                .getQuery();
        adList = selectQuery.fetch(record -> {
        	Integer adId = record.get(0,Integer.class);
    		return adId;
    	});
		
		return adList;
	}
    
    @SuppressWarnings("unchecked")
    public Page<SpaceConfInfoSearchResponse> list(SpaceConfInfoSearchRequest searchRequest, Pageable pageable){
    	
    	SelectQuery selectQuery = getSelectQuery(searchRequest);
		return search(selectQuery, pageable, record ->{
			SpaceConfInfoSearchResponse response = new SpaceConfInfoSearchResponse();
			response.setId(record.get(0,Long.class));
			response.setType(record.get(1,String.class));
			response.setName(record.get(2,String.class));
			response.setCode(record.get(3,Integer.class));
			response.setKindname(record.get(4,String.class));
			return response;
		});
    }
    
    public SelectQuery getSelectQuery(SpaceConfInfoSearchRequest searchRequest) {
		Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        List<GroupField> groupFields = new ArrayList<>();
        
        selectFields.add(COMM_SYS_CODE.ID);
        selectFields.add(COMM_SYS_CODE.TYPE);
        selectFields.add(COMM_SYS_CODE.NAME);
        selectFields.add(COMM_SYS_CODE.CODE);
        selectFields.add(COMM_SYS_CODE.KINDNAME);
        condition = condition.and(COMM_SYS_CODE.TYPE.eq(SPACE_TYPE));
        if (searchRequest.getCode() != null) {
            condition = condition.and(COMM_SYS_CODE.CODE.eq(searchRequest.getCode()));
        }
        if (StringUtils.isNotEmpty(searchRequest.getKindname())) {
        	condition = condition.and(COMM_SYS_CODE.KINDNAME.eq(searchRequest.getKindname()));
        }
		
		return dslContext.select(selectFields)
                .from(COMM_SYS_CODE)
                .where(condition).orderBy(COMM_SYS_CODE.CODE.desc())
                .getQuery();
	} 
    

    @Override
    protected TableImpl getTable() {
        return COMM_SYS_CODE;
    }

}
